import React from 'react';
import {Button, Dropdown, Menu, message, Modal, Space} from 'antd';
import {
  EditOutlined,
  DeleteOutlined,
  MoreOutlined,
  PlayCircleOutlined,
  PauseOutlined
} from '@ant-design/icons';
import { useModel } from 'umi';
import {API} from "@/services/typings";

interface OperationButtonsProps {
  record: API.ISysJob;
}

const OperationButtons: React.FC<OperationButtonsProps> = ({ record }) => {
  // const { setCurrentRow, setFormType, setModalVisible } = useModel('jobManagement');

  const handleEdit = () => {
      // setCurrentRow(record);
      // setFormType('edit');
      // setModalVisible(true);
  };

  const handleDelete = () => {
    Modal.confirm({
      title: '确认删除定时任务?',
      content: `确定要删除任务 "${record.jobName}" 吗?`,
      onOk: async () => {
        try {
          // 这里替换为实际的API请求
          console.log('删除任务:', record.jobId);
          // await request(`/api/jobs/${record.jobId}`, { method: 'DELETE' });
          message.success('删除成功');
          // 刷新表格
          // actionRef.current?.reload();
        } catch (error) {
          message.error('删除失败');
        }
      },
    });
  };

  const handleExecute = () => {
    Modal.confirm({
      title: '确认立即执行任务?',
      content: `确定要立即执行任务 "${record.jobName}" 吗?`,
      onOk: async () => {
        try {
          // 这里替换为实际的API请求
          console.log('执行任务:', record.jobId);
          // await request(`/api/jobs/${record.jobId}/execute`, { method: 'POST' });
          message.success('任务已触发执行');
        } catch (error) {
          message.error('任务执行失败');
        }
      },
    });
  };

  const menu = (
    <Menu>
      <Menu.Item key="execute" icon={<PlayCircleOutlined />} onClick={handleExecute}>
        立即执行
      </Menu.Item>
      <Menu.Item key="pause" icon={<PauseOutlined />} disabled={record.status === '1'}>
        暂停任务
      </Menu.Item>
    </Menu>
  );

  return (
    <Space>
      <Button type="link" size="small" icon={<EditOutlined />} onClick={handleEdit}>
        编辑
      </Button>

      <Button type="link" size="small" danger icon={<DeleteOutlined />} onClick={handleDelete}>
        删除
      </Button>

      <Dropdown overlay={menu} placement="bottomRight">
        <Button type="text" size="small" icon={<MoreOutlined />} />
      </Dropdown>
    </Space>
  );
};

export default OperationButtons;
